"""
1.用js控制日期选择
2.用js滑动滑块
3.上传文件
"""
import time

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.12306.cn/index/")

# 1.用JS控制日期选择
# js_code = "imput = document.getElementById('train_date');imput.readOnly = false;imput.value = '2022-09-06'"
# driver.execute_script(js_code)
# time.sleep(3)

# 把js跟python混用
input_elem = driver.find_element(By.ID, "train_date")
js_code = "arguments[0].readOnly = false;arguments[0].value = '2022-09-06'"
driver.execute_script(js_code, input_elem)
time.sleep(2)


# 2.用JS控制滑块
# window.scrollBy滚动多少个像素点

# window.scrollTo滚动到哪个像素点
# window.scrollTo(0,500) X,Y坐标，X表示横向滚动，Y表示纵向滚动
# window.scrollTo(0,document.body.scrollHeight) 滚动到底部
# window.scrollTo(0,document.body.scrollHeight / 2) 滚动到1/2，中间
# 将元素滚动到可视范围
el = driver.find_element(By.XPATH, "//h2[text()='友情链接']")
el.location_once_scrolled_into_view
time.sleep(2)
driver.quit()

driver1 = webdriver.Chrome()
url = ("file:///C:/Users/Mr%20MING/PycharmProjects/%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/learn-git/js%E6%93%8D%E4%BD%9C/%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6%E5%89%AF%E6%9C%AC.html")
driver1.get(url)



# 3.文件上传操作
# input可以通过send_keys,发送文件路径
f = driver1.find_element(By.ID, "mfile")
f.send_keys(r"C:\Users\Mr MING\PycharmProjects\自动化测试\learn-git\js操作\js操作.py")
time.sleep(2)

driver1.quit()